﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SimulationDataGenerator
{
    public partial class FormMain : Form
    {
        private JCIDB db = new JCIDB();
        public FormMain()
        {
            InitializeComponent();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            try
            {
                Current_Alarm currentAlarm = new Current_Alarm();
                //currentAlarm.Robot_ID = 1;
                currentAlarm.Robot = db.Robot.Where(a => a.ID == 1).FirstOrDefault();
                currentAlarm.Alarm_Code = "76";
                currentAlarm.Alarm_Message = "DNET-055 Board or network error: Bd 1";
                currentAlarm.Alarm_Severity = "STOP.G";
                currentAlarm.Alarm_Cause = "DNET-055 Board or network error: Bd 1";
                currentAlarm.Time = System.DateTime.Now;
                db.Current_Alarm.Add(currentAlarm);

                for (int i = 0; i < 1000; i++)
                {
                    Part_Unit partUnit = new Part_Unit();
                    partUnit.Batch = System.DateTime.Now.Date.ToString("yyyyMMdd");
                    partUnit.Part_Name = i % 2 == 0 ? "MILL" : "MILL";
                    partUnit.Part_Serial = "SN-" + System.DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    //partUnit.State_ID = i % 100 == 0 ? 2 : 1;
                    partUnit.Part_State = i % 100 == 0 ? db.Part_State.Where(a => a.ID == 2).FirstOrDefault() : db.Part_State.Where(a => a.ID == 1).FirstOrDefault();
                    //partUnit.Robot_ID = i % 2 == 0 ? 8 : 10;
                    partUnit.Robot = i % 2 == 0 ? db.Robot.Where(a => a.ID == 8).FirstOrDefault() : db.Robot.Where(a => a.ID == 10).FirstOrDefault();
                    partUnit.Start_Time = System.DateTime.Now;
                    partUnit.End_Time = System.DateTime.Now.AddMilliseconds(5000);
                    partUnit.Insert_Time = System.DateTime.Now.AddMilliseconds(5000);
                    partUnit.Quality_Grade = i % 100 == 0 ? 1 : 2;
                    db.Part_Unit.Add(partUnit);

                    Pass_Value_History passValueHistory = new Pass_Value_History();
                    passValueHistory.Part_Serial = partUnit.Part_Serial;
                    passValueHistory.Pass_No = i % 2 == 0 ? 1 : 2;
                    passValueHistory.Para_Value1 = i % 2 == 0 ? 70 : 100;
                    passValueHistory.Para_Value2 = i % 2 == 0 ? 50 : 80;
                    passValueHistory.Para_Value3 = i % 2 == 0 ? 8 : 10;
                    passValueHistory.Weave_Value1 = i % 2 == 0 ? 1 : 2;
                    passValueHistory.Weave_Value2 = i % 2 == 0 ? 4 : 5;
                    passValueHistory.Weave_Value3 = i % 2 == 0 ? 0 : 1;
                    passValueHistory.Weave_Value4 = i % 2 == 0 ? 1 : 0;
                    passValueHistory.Data_Voltage = "0,0,0,0,0,0,0,3211,21627,21627,21627,21627,21627,21627,21627,32178,7798,10747,12845,13500,13631,14286,13500,13762,11862,11403,12713,13893,13959,13434,11993,13107,11993,12779,13565,12582,12713,12910,12713,12910,12976,12845,12648,12845,12910,12320,12517,13434,12910,13500,12648,12517,12582,12910,12582,12779,13565,12058,12779,13369,12779,12451,12517,12648,12910,13041,11468,12779,13500,12582,12976,12255,12582,12189,13303,13631,14614,13107,11993,13041,11927,14286,13369,13631,12779,13238,12386,13238,12386,13041,13303,12845,13434,12713,12124,13172,11927,10944,13238,13565,13697,13172,12582,12845,12189,11730,13959,13762,12386,12386,10944,13500,13631,12124,12517,12517,11993,13041,13369,12845,13172,11927,12648,13303,12648,10420,13565,13631,13172,13303,12255,12189,13238,12451,12451,13631,12845,13107,12845,11796,13500,11534,12517,13828,13500,13107,12320,13238,10944,12779,13697,13565,13107,12582,11730,13762,12910,13434,11993,13303,12582,13172,12779,12845,13434,12845,12648,13238,11272,12320,14286,13631,12648,11141,13303,11862,14155,13369,13041,12648,12910,12517,11862,13762,12320,12976,12845,13893,12582,12517,13893,12779,13238,12255,13369,12582,10878,10878,14155,12779,13107,12779,12976,12058,13303,12517,10813,11927,13303,13238,13303,13565,12255,11993,12255,13762,13434,12976,13369,12845,12779,11534,12058,13303,13434,12320,11403,13500,12648,13697,12648,13172,12386,12320,11796,13369,13828,12845,12386,12713,12845,13107,13631,12320,14221,14745,11141,13369,11403,11403,13631,13828,13959,11468,12124,11468,13369,12976,10354,13565,12910,12582,12386,13107,13434,13565,13041,13500,12386,12058,11730,13893,13893,12845,12582,12124,13565,13238,12845,11403,13631,14024,13631,12255,12124,13172,13238,13041,10551,13303,13565,12386,13828,12124,13697,13500,13172,11993,12124,12517,12451,12255,12648,14090,13172,13631,12451,12779,13369,12517,12124,13107,12451,13500,12713,12386,12386,13631,13172,13828,12910,12451,12648,12517,10223,13107,13107,13565,12976,12779,12648,11665,13172,12255,13369,11534,13238,13565,13434,12976,11927,11862,13893,13303,13107,12517,12517,14024,12976,13631,12648,12517,13369,10551,14221,13303,13107,12451,11468,12910,12124,12189,13238,12582,13434,13303,12255,11534,12451,13762,13697,12124,12517,12779,12451,12124,12910,12910,13172,12124,13631,12779,13238,12713,12124,13762,13697,13238,12451,12648,12386,12648,12779,12779,13434,13500,13369,13565,12189,12582,13107,12976,13369,13893,13107,12582,12517,13041,13303,12910,13172,12255,13041,13500,13238,11665,11927,13369,13303,12976,13893,12189,12517,12976,12320,11927,13828,13369,12517,13172,13500,11927,12189,14417,14286,13107,11993,13828,13500,14680,13434,11468,11599,14483,14483,14221,12976,12976,12582,11927,11796,13107,13041,12713,13303,13041,11534,12910,13238,13041,13041,13041,11993,13303,13041,11206,12648,13697,12713,13303,12517,13369,13434,12648,12779,13762,13172,12320,11534,13238,12648,13303,12713,13107,12648,10158,13238,14745,13041,13631,12779,12189,12124,12976,13369,12648,13041,13107,12451,13238,13434,13434,13500,13238,13369,12845,11993,13107,13303,13172,12713,13041,12582,12648,12713,12713,12582,12779,13303,13828,12582,12648,13369,13041,12320,12845,13697,13041,11730,13107,12648,13303,11468,14155,13565,13303,12386,13172,11075,13369,12976,11927,12124,13959,13697,13172,13107,12189,12255,13107,13697,12910,12189,12648,12255,12124,12779,13303,13434,13238,12648,11927,12451,12648,13697,12255,12320,14221,13041,12451";
                    passValueHistory.Data_Current = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6225,16056,8400,8519,7751,7012,8323,4671,4521,4587,9961,10420,8591,8388,8192,5046,7733,6160,9043,8650,8716,5832,8454,6684,8454,8060,7602,8007,7340,8454,8650,8847,8650,7077,8388,7995,8585,8519,8519,7733,7995,8388,6094,8519,8192,6750,8716,6553,7733,8585,8781,6815,9502,8454,5242,8519,7012,8716,8978,9764,8781,8519,6422,4980,8192,9175,10027,7798,8192,6881,8519,5636,8257,9437,8847,8192,8192,8454,5767,8341,6619,8978,9592,8912,8591,8323,8257,6946,8519,6946,9568,8585,8388,5505,7929,7602,8323,8454,5439,8138,6291,10027,8650,8585,5898,7995,5898,8585,8192,7952,11141,8585,8323,7864,8323,5636,8847,8650,8978,9312,8585,5701,8257,8454,7143,8388,9437,9306,8716,5507,8519,6291,8323,6619,11993,9109,8781,8126,8257,9830,8847,5636,8192,9699,8126,5439,8454,8192,9568,8847,8650,5832,8454,7602,10598,8716,8192,5439,6684,9811,10223,8847,5963,4980,10237,9240,5636,8781,8650,7274,7274,7471,5898,7995,9062,8519,6291,7208,6881,6553,7143,6881,11927,9240,5505,8388,7208,8388,7274,8323,5505,6422,10092,9764,8466,7667,7995,5177,5636,9175,10354,8519,6553,8323,6684,8126,10289,10092,7929,8257,5439,9633,8978,9240,8388,5832,7667,5767,8716,10223,8912,8192,5308,5242,9371,8912,5832,8192,10092,8126,8192,6553,8192,9306,9527,8847,8323,7471,6160,8388,9895,7864,6225,7536,8847,9699,8781,8919,8978,9287,8716,5373,7929,5439,8126,10027,8847,8454,5898,6291,10027,8388,6619,6356,9175,8847,8454,7733,5832,9961,8847,6881,6750,8781,8716,8388,5505,8126,10223,10485,7340,7929,5505,7077,6750,8585,9699,9175,9830,8192,8192,7929,7733,8257,6381,9437,8781,8781,8585,8978,8585,8912,8716,8912,8585,5373,4980,6946,7798,8126,11337,8716,8454,6619,6881,5636,8847,8585,7340,8323,8126,9895,8454,8257,7864,9568,10467,9371,8126,7929,8585,10682,10223,6160,7798,6553,9502,8650,8585,10878,6881,7143,6291,8192,8519,6291,9895,8257,8650,8716,6225,5439,11010,9175,8781,7995,8781,6160,7929,8458,8454,8519,9240,8388,9175,8388,5701,8323,6619,8912,8781,8519,5767,4980,7471,8650,8847,9830,9240,8656,8454,7864,7798,5177,7995,10092,9437,9109,8454,7864,8388,7256,8388,8716,8650,8650,8800,8060,8257,7602,9175,9080,8650,8388,6750,7864,5505,9043,8650,10223,9633,8110,7536,5111,8257,8257,11386,8585,10027,8650,8126,5570,8388,8060,8257,8192,7667,9437,11927,8847,8454,5439,7798,6356,7602,11075,9109,8585,5898,8060,8388,5767,9830,8519,7208,7864,8323,5439,8192,5308,9568,10223,8519,5308,8257,8323,7077,6619,8292,8847,8585,7864,5898,9895,8519,6488,8388,8519,7667,6291,9437,9830,10878,6553,8060,7340,6029,5898,8585,8454,6750,8357,7536,9172,8585,8781,8519,8257,8257,7536,8126,9922,8847,8454,8192,7208,8323,6225,8126,6356,8454,8650,8519,8912,8519,6619,8323,7208,8388,6750,9502,8519,6619,9240,8323,7274,8716,10158,8847,8126,8192,5308,7602,8495,8388,8519,9830,9043,8847,8323,5111,5988,4849,9961,8781,8323,8327,5505,8192,5701,9306,10657,9043,7364,7536,5505,7864,7077,8407,8388,9043,8853,8650,8388,8781";
                    passValueHistory.Start_Time = partUnit.Start_Time;
                    passValueHistory.End_Time = partUnit.End_Time;
                    passValueHistory.Insert_Time = partUnit.Insert_Time;
                    passValueHistory.Current_Standard = 0;
                    passValueHistory.Current_Standard_High_Percent = 0;
                    passValueHistory.Current_Standard_Low_Percent = 0;
                    passValueHistory.Voltage_Standard = 0;
                    passValueHistory.Voltage_Standard_High_Percent = 0;
                    passValueHistory.Voltage_Standard_Low_Percent = 0;
                    passValueHistory.Quality_Grade = i % 100 == 0 ? 1 : 2;
                    passValueHistory.Voltage_Real_Over_Percent = 0;
                    passValueHistory.Current_Real_Over_Percent = 0;
                    db.Pass_Value_History.Add(passValueHistory);

                    if (i % 100 == 0)
                    {
                        Robot_State_Change_History robotStateChangeHistory = new Robot_State_Change_History();
                        //robotStateChangeHistory.Robot_ID = i % 2 == 0 ? 8 : 10;
                        robotStateChangeHistory.Robot = i % 2 == 0 ? db.Robot.Where(a => a.ID == 8).FirstOrDefault() : db.Robot.Where(a => a.ID == 10).FirstOrDefault();
                        switch (i % 3)
                        {
                            case 0:
                                robotStateChangeHistory.Original_State_Type = 2;
                                robotStateChangeHistory.Changed_State_Type = 4;
                                robotStateChangeHistory.ChangeDateTime = System.DateTime.Now.Date.AddHours(1 + i);
                                robotStateChangeHistory.Period = 100;
                                break;
                            case 1:
                                robotStateChangeHistory.Original_State_Type = 3;
                                robotStateChangeHistory.ChangeDateTime = System.DateTime.Now.Date.AddHours(1 + i);
                                robotStateChangeHistory.Period = 200;
                                break;
                            case 2:
                                robotStateChangeHistory.Original_State_Type = 4;
                                robotStateChangeHistory.ChangeDateTime = System.DateTime.Now.Date.AddHours(1 + i);
                                robotStateChangeHistory.Period = 10000;
                                break;
                        }
                        db.Robot_State_Change_History.Add(robotStateChangeHistory);
                    }

                }
                db.SaveChanges();
                this.richTextBox1.AppendText(System.DateTime.Now+" Done");
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

    }
}
